﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using KamLib.Web.MVC;


namespace KamLib.Web.Bootstrap
{


    partial class BootstrapHelper
    {

        #region For MVC

        public DropDownMenu DropDownMenu(Action<DropDownMenu.Setter> menuSetter)
        {
            return DropDownMenu((DropDownMenu)menuSetter);
        }
        public DropDownMenu DropDownMenu(params Action<DropDownItem.Setter>[] itemsSetter)
        {
            return DropDownMenu(new DropDownMenu().Items(itemsSetter));
        }
        
        public DropDown DropDown(Action<DropDown.Setter> setter)
        {
            return DropDown((DropDown)setter);
        }
        public DropDown DropDown(Action<Link.Setter> titleSetter, Action<DropDownMenu.Setter> menuSetter)
        {
            return DropDown(new DropDown().Title(titleSetter).Menu(menuSetter));
        }

        public DropDown DropDownLI(Action<DropDown.Setter> setter)
        {
            return DropDown(setter).Tag("li");
        }
        public DropDown DropDownLI(Action<Link.Setter> titleSetter, Action<DropDownMenu.Setter> menuSetter)
        {
            return DropDown(titleSetter, menuSetter).Tag("li");
        }

        #endregion


        #region Renders

        [Browsable(false)]
        public virtual DropDown DropDown(DropDown el)
        {
            if (el.Title.no()) return el;

            el.TagIf("div").Class("dropdown");

            el.Title.TagIf("a").Class("dropdown-toggle").Attrs(@"role=""button"" data-toggle=""dropdown"" href=""#""");
            el.Title.ApplyParams(el.Title.Text);
            DropDownMenu(el.Menu);

            if (el.HasMenu)
                el.Title.Text.Value += @" <b class=""caret""></b>";

            el.Renderer = sb =>
            {
                el.Title.Render(sb);
                el.Menu.Render(sb);
            };

            return el;
        }
        

        [Browsable(false)]
        public virtual DropDownMenu DropDownMenu(DropDownMenu el)
        {
            if (el.Items.no()) return el;

            el.Tag("ul").Class("dropdown-menu").Attrs(@"role=""menu""");

            foreach (var item in el.Items)
            {
                DropDownItem(item);
            }

            el.Renderer = sb =>
            {
                foreach (var item in el.Items)
                {
                    item.Render(sb);
                }
            };

            return el;
        }


        [Browsable(false)]
        public virtual DropDownItem DropDownItem(DropDownItem el)
        {
            el.Tag = "li";
            if (el.Text.no())
            {
                el.Class("divider");
                return el;
            }

            if (!el.Enabled) el.Class("disabled");

            if (el.HasMenu)
            {
                el.Class("dropdown-submenu");
                DropDownMenu(el.Menu);
            }

            el.Renderer = sb =>
            {
                sb.Append(@"<a tabindex=""-1""");
                sb.Append(@" href=""" + (el.Url ?? "#") + @""">");
                sb.Append(el.Text);
                sb.Append("</a>");
                if (el.HasMenu) el.Menu.Render(sb);
            };

            return el;
        }

        #endregion

    }


}
